package pl.edu.agh.pros.proxy.app;

import pl.edu.agh.pros.proxy.logic.dao.ProviderInfoDAO;
import pl.edu.agh.pros.proxy.model.ProviderInfo;
import pl.edu.agh.pros.proxy.view.TabSheetLayout;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.vaadin.Application;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.BaseTheme;

public class ProsServer extends Application {

	private static final long serialVersionUID = 9023652649364744340L;

	private static final String ROS_PROXY_SERVER = "ROS Proxy Server";
	private static final ThemeResource logoutIcon = new ThemeResource("images/logout.png");

	@Override
	public void init() {
		buildMainLayout();
	}

	@SuppressWarnings("serial")
	private void buildMainLayout() {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		setLogoutURL(userService.createLogoutURL("/"));
		ProviderInfo providerInfo = ProviderInfoDAO.getInstance().getProviderInfo(user.getEmail());

		setMainWindow(new Window(ROS_PROXY_SERVER));
		setTheme("pros");
		VerticalLayout layout = new VerticalLayout();
		layout.setSizeFull();

		Button logoutButton = new Button("Logout");
		logoutButton.setIcon(logoutIcon);
		logoutButton.setStyleName(BaseTheme.BUTTON_LINK);
		logoutButton.addListener(new Button.ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				getMainWindow().getApplication().close();
			}
		});

		layout.addComponent(logoutButton);
		layout.setComponentAlignment(logoutButton, Alignment.BOTTOM_RIGHT);
		TabSheetLayout tabSheetLayout = new TabSheetLayout(providerInfo);
		layout.addComponent(tabSheetLayout);
		layout.setComponentAlignment(tabSheetLayout, Alignment.TOP_CENTER);
		layout.setMargin(true, true, true, true);

		getMainWindow().setContent(layout);
	}
}